/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.nell;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.drawable.PictureDrawable;
import android.webkit.WebView;
import android.webkit.WebView.PictureListener;
import java.io.File;
import java.io.FileOutputStream;

/**
 *
 * @author Administrator
 */
public class ScreenshotListener implements PictureListener {

    private File dir;
    public ScreenshotListener() {
        super();
        String ssp = ApplicationSettings.getScreenshotPath();
        dir = new File(ssp);
        dir.mkdirs();

    }

    public void onNewPicture(WebView wv, Picture picture) {
        if (picture != null && ApplicationSettings.isRecordScreen()) {
            String filename = dir + "/" + System.currentTimeMillis()+ ".png";
            try {
                Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
                FileOutputStream out = new FileOutputStream(filename);
                bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pictureDrawable.getPicture());
        return bitmap;
    }
}
